package com.example.smart_city_0829.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.entity.Base;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class RegisterActivity extends BaseActivity {

    private EditText userName;
    private EditText password;
    private EditText phoneNumber;
    private RadioGroup rb;
    private Button register;
    private RadioButton man;
    private RadioButton woman;

    private int sex = 0;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        userName = (EditText) findViewById(R.id.user_name);
        password = (EditText) findViewById(R.id.password);
        phoneNumber = (EditText) findViewById(R.id.phone_number);
        rb = (RadioGroup) findViewById(R.id.rb);
        register = (Button) findViewById(R.id.register);
        man = (RadioButton) findViewById(R.id.man);
        woman = (RadioButton) findViewById(R.id.woman);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.man){
                    sex = 0;
                }else {
                    sex = 1;
                }
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String username = userName.getText().toString();
                final String password = RegisterActivity.this.password.getText().toString();
                final String phoneNumber = RegisterActivity.this.phoneNumber.getText().toString();
                final HashMap<String, Object> params = new HashMap<>();
                params.put("userName",username);
                params.put("password",password);
                params.put("phonenumber",phoneNumber);
                params.put("sex",sex);
                final RequestBody requestBody = RequestBody.create(gson.toJson(params), MediaType.parse("application/json"));
                api.register(requestBody).enqueue(new NetCallBack<Base>() {
                    @Override
                    public void onResponse(Call<Base> call, Response<Base> response) {
                        final String msg = response.body().getMsg();
                        showToastShort(msg);
                        if (msg.contains("成功")){
                            RegisterActivity.this.finish();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "欢迎注册";
    }
}